package com.wq.myspring.eventtest;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
 * @PackageName: com.wq.myspring.eventtest
 * @ClassName MyTest5
 * @Description: TODO spring event (观察者模式)
 * @Author Wenqiang
 * @Date 2021/07/16 23:34
 * @Version V1.0
 */
public class MyTest5 {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ListenerConfig.class);
		context.publishEvent(new ApplicationEvent("张三发布新单曲《颤三的歌》") {
			@Override
			public Object getSource() {
				Object source = super.getSource();
				return source;
			}
		});
		context.close();

	}
}

@ComponentScan("com.wq.myspring.eventtest")
class ListenerConfig {

}

@Component
class MyListener implements ApplicationListener {
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		System.out.println("接收到事件: " + event);
	}
}
